function setToken(token, expirationTimeSeconds) {
    // Set the expiration time based on the provided number of seconds
    const expirationTime = new Date().getTime() + expirationTimeSeconds * 1000;

    // Store the token and its expiration time in localStorage
    localStorage.setItem('token', JSON.stringify({ value: token, expiresAt: expirationTime }));

    // Set a timeout to automatically delete the token after the expiration time
    setTimeout(() => {
        localStorage.removeItem('token');
    }, expirationTimeSeconds * 1000);
}

function getToken() {
    // Retrieve the cached token from localStorage
    const cachedToken = JSON.parse(localStorage.getItem('token'));

    if (cachedToken && cachedToken.expiresAt > new Date().getTime()) {
        // The token is still valid, return it
        return cachedToken.value;
    } else {
        // The token has expired or is not set, remove it from localStorage
        localStorage.removeItem('token');
        return null;
    }
}

export default {
    getToken,
    setToken
}